3D Graphics Programming with QuickDraw 3D 1.5.4
Previous | QD3D Book | Overview | Chapter Contents | Next |
As you've seen, an illumination shader determines the effects of a view's group of lights on the objects in a model. In order for the lights to have any effect, you must attach an illumination shader to the view. QuickDraw 3D provides three types of illumination shaders.
The Lambert illumination shader implements an illumination model based on the diffuse reflection (also called the Lambertian reflection) of a surface. Diffuse reflection is characteristic of light reflected from a dull, nonshiny surface. Objects illuminated solely by diffusely reflected light exhibit an equal light intensity from all viewing directions. Figure 1 shows an object illuminated using the Lambert illumination shader. See also Color Plate 4 at the beginning of this book.
Figure 1 Effects of the Lambert illumination shader
For a point on a surface, the Lambert illumination provided by i distinct lights is given by the following equation:
Here, I a is the intensity of the ambient light, and k a is the ambient coefficient. O d is the diffuse color of the surface of the object being illuminated. N is the surface normal vector at the point whose illumination is being evaluated, and L i is a normalized vector indicating the direction to the i th light source. Notice that if the dot product ( N L i ) is 0 for a particular light (that is, if N and L i are perpendicular), that light contributes nothing to the illumination of the point. I i is the intensity of the i th light source, and k d is the diffuse coefficient of the surface being illuminated (that is, the level of diffuse reflection of the surface).
As you can see, the intensity of the light reflected by a point on a surface depends solely on the ambient light and the diffuse reflection of the surface at that point.
QuickDraw 3D does not currently provide a way to set the value of the diffuse coefficient of a surface directly. Instead, you must use the product k d O d as the surface's diffuse color. You specify a diffuse color by inserting an attribute of type kQ3AttributeTypeDiffuseColor into the surface's attribute set.
The Phong illumination shader implements an illumination model based on both diffuse reflection and specular reflection of a surface. Specular reflection is characteristic of light reflected from a shiny surface, where a bright highlight appears from certain viewing directions. Figure 2 shows an object illuminated using the Phong illumination shader. See also Color Plate 4 at the beginning of this book.
Figure 2 Effects of the Phong illumination shader
For a point on a surface, the Phong illumination provided by i distinct lights is given by the following equation:
Notice that the Phong illumination equation is simply the Lambert illumination equation with an additional summand to account for specular reflection. Here, R is the direction of reflection and V is the direction of viewing. The exponent n is the specular reflection exponent, and k s is the specular reflection coefficient. The specular reflection exponent determines how quickly the specular reflection diminishes as the viewing direction moves away from the direction of reflection. In other words, the specular reflection exponent determines the size of the specular highlight (a bright area on the surface of the object caused by specular reflection). When the value of n is small, the size of the specular highlight is large; as n increases, the size of the specular highlight shrinks.
Note that setting the specular reflection exponent to 0 results in no specular reflection (because n 0 = 1 for any number n ). Moreover, values between 0 and 1 reduce the amount of specular reflection. In general, the specular reflection exponent should be a value greater than or equal to 1.
The specular coefficient (or specular reflection coefficient ), symbolized by k s in the equation above, indicates the level of the object's specular reflection. It controls the overall brightness of the specular highlight, independent of the brightness of the light sources and the direction of viewing.
Figure 3 shows an object illuminated using a variety of values for the specular reflection exponent and the specular coefficient. In this figure, the specular reflection exponent increases from left to right, resulting in a smaller specular highlight. In addition, the specular coefficient increases from top to bottom, resulting in a brighter specular highlight.
A surface's specular reflection coefficient is also called its specular control. You specify a specular reflection coefficient by inserting an attribute of type kQ3AttributeTypeSpecularControl into the surface's attribute set.
Figure 3 Phong illumination with various specular exponents and coefficients
The null illumination shader ignores the lights in a view's light group and configures the renderer to draw all objects using only the diffuse colors of those objects. The net effect of the this shader is to draw objects as if the only light source was an ambient light at full intensity. Figure 4 shows an object illuminated using the null illumination shader.
Figure 4 Effects of the null illumination shader
For any point on a surface, the null illumination is given by the following equation:
Here, O d is the diffuse color of the surface of the object being illuminated. As you can see, when the null illumination shader is active, all facets of an object are drawn the same color (unless different facets have attribute sets that override the diffuse color of the object).
Previous | QD3D Book | Overview | Chapter Contents | Next |